package com.xinya.taobaosale.ui.activity;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.xinya.taobaosale.R;
import com.xinya.taobaosale.model.domain.TicketBean;
import com.xinya.taobaosale.ui.presenter.impl.TicketPresenterImpl;
import com.xinya.taobaosale.ui.presenter.interfaces.ITicketCallback;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class TicketActivity extends BaseActivity implements ITicketCallback {

    private static final String TAG = "TicketActivity";
    @BindView(R.id.iv_ticket_back)
    ImageView ivTicketBack;
    @BindView(R.id.et_ticket_model)
    EditText etTicketModel;
    @BindView(R.id.tv_ticket_get)
    TextView tvTicketGet;
    @BindView(R.id.iv_ticket_pic)
    ImageView ivTicketPic;

    boolean hasInstalledTaoBao = false;

    @Override
    protected int getContentViewByViewId() {
        return R.layout.activity_ticket;
    }

    @Override
    protected void initView() {
        hasInstalledTaoBao = hasInstallTaoBao();
        if (!hasInstalledTaoBao) {
            tvTicketGet.setText(R.string.copy_string_text);
//            Toast.makeText(this, "未检测到淘宝app", Toast.LENGTH_SHORT).show();
        } else {
            tvTicketGet.setText(R.string.ticket_get_text);
            Log.d(TAG, "initView: 手机检测到已经安装了淘宝客户端");
        }
    }

    private boolean hasInstallTaoBao() {
        List<PackageInfo> installedPackages = getPackageManager().getInstalledPackages(0);
        for (PackageInfo packageInfo : installedPackages) {
            if (packageInfo.packageName.equalsIgnoreCase("com.taobao.taobao")) {
                return true;
            }
        }
        return false;
    }


    @Override
    protected void initEvent() {

    }

    @Override
    public void getTicket(TicketBean bean, String pic_url) {
        Glide.with(this).load("https:" + pic_url).into(ivTicketPic);
        String data = bean.getTbk_tpwd_create_response().getData().getModel();
        String result = data.substring(data.indexOf('￥'), data.lastIndexOf('￥') + 1);
        if (TextUtils.isEmpty(result)) {
            return;
        }
        etTicketModel.setText(result);

        Log.d(TAG, "getTicket: " + bean.getTbk_tpwd_create_response().getData().getModel());
    }

    @Override
    public void onLoadSuccess(int id) {

    }

    @Override
    public void onLoadError(int id) {
        Toast.makeText(this, "网络错误,请检查网络", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onLoadEmpty(int id) {
    }

    @Override
    public void onLoading(int id) {

    }

    @Override
    protected void registerPresenter() {
        TicketPresenterImpl.INST.register(this);
    }

    @Override
    protected void unRegisterPresenter() {
        TicketPresenterImpl.INST.unRegister(this);
    }


    @OnClick({R.id.iv_ticket_back, R.id.tv_ticket_get})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_ticket_back:
                finish();
                break;
            case R.id.tv_ticket_get:
                // 进行app跳转
                String data = etTicketModel.getText().toString().trim();
                if (TextUtils.isEmpty(data)) {
                    return;
                }
                // 复制
                ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                cm.setPrimaryClip(ClipData.newPlainText("taobao_code", data));

                // 判断是否有相关app, 没有就仅复制
                if (!hasInstalledTaoBao) {
                    Toast.makeText(this, "复制成功！", Toast.LENGTH_SHORT).show();
                    return;
                }
                // 跳转
                Intent intent = new Intent();
                ComponentName componentName = new ComponentName("com.taobao.taobao", "com.taobao.tao.TBMainActivity");
                intent.setComponent(componentName);
                startActivity(intent);
                break;
        }
    }

}